package com.jack.leetcode.每日一题;

/***
 * @author zhangjie
 * @date 2020/4/10 19:54
 */
public class 翻转字符串里的单词 {

    public static void main(String[] args) {
        String s = "the sky is blue";
        System.out.println(reverseWords(s));
        s = "  the sky is blue";
        System.out.println(reverseWords(s));
        s = "  the sky is     blue";
        System.out.println(reverseWords(s));
        s = "the     sky is blue";
        System.out.println(reverseWords(s));
        s = "    ";
        System.out.println(reverseWords(s));
    }

    public static String reverseWords(String s) {
        s = s.trim();
        StringBuilder sb = new StringBuilder();
        if(s.length() == 0){
            return "";
        }
        String[] splits = s.split(" ");
        for(int i= splits.length-1;i>=0;i--){
            if("".equals(splits[i].trim())){
                continue;
            }
            sb.append(splits[i]).append(" ");
        }
        return sb.toString().trim();
    }
}
